package com.diver.flinkdemo.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diver.flinkdemo.dto.xxt.C40DataVO;
import com.diver.flinkdemo.entity.xxt.MessageSend;
import com.diver.flinkdemo.mapper.mysqlMapper.MessageSendMapper;
import com.diver.flinkdemo.service.IMessageSendService;
import com.diver.flinkdemo.service.IUserService;
import com.diver.flinkdemo.utils.DateUtil;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import com.diver.flinkdemo.utils.UUIDUtil;
import com.diver.flinkdemo.utils.UnicodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hesc
 * @since 2022-03-02
 */
//@Service
@Slf4j
public class MessageSendServiceImpl extends ServiceImpl<MessageSendMapper, MessageSend> implements IMessageSendService {
//    private KafkaClient kafkaClient;

    private MessageSendMapper mapper;

    private IUserService userService = new UserService() ;
    private SqlSession session;

    @Override
    public List<MessageSend> queryMessageList(String imei, String userId, Integer limit) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(MessageSendMapper.class);
            return mapper.queryMessageList(imei, userId, limit);
            //return null;
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public String queryNickname(String userId, String imei) {
        session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
        mapper = session.getMapper(MessageSendMapper.class);
        String result = mapper.queryNickname(userId, imei);
        //String result = null;
        if(StringUtils.isBlank(result)) {
            result = userService.queryNickname(userId);
        }
        return result;
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }

    @Override
    public void updateMessageStatus(C40DataVO c40DataVO) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(MessageSendMapper.class);
            String imei = c40DataVO.getImei();
            String tagId = c40DataVO.getSmsUid();
            if(StringUtils.isBlank(tagId)) {
                tagId = c40DataVO.getTaskId();
            }
            String status = c40DataVO.getStatus();
            log.info("发送信息回调，数据:{}", c40DataVO);
            if(StringUtils.isEmpty(imei) || StringUtils.isEmpty(tagId)) {
                return;
            }
            MessageSend messageSend = mapper.selectOne(new QueryWrapper<MessageSend>().eq("imei", imei).eq("tag_id", tagId).eq("del_flag", "0"));
            if(messageSend == null) {
                return;
            }
            if("1".equals(status)) {
                messageSend.setIssuedMsgStatus(1);
            } else {
                messageSend.setIssuedMsgStatus(2);
            }
            messageSend.setUpdateTime(new Date());
            mapper.updateById(messageSend);
        } finally {
            endOfSession(session);
        }
    }
}
